{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入本项目所需要的包\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from sklearn import datasets\n",
    "\n",
    "from sklearn import svm\n",
    "\n",
    "# 使用交叉验证的方法，把数据集分为训练集合测试集\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
       "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
       "       1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
       "       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
       "       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris = datasets.load_iris()\n",
    "iris.target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 加载数据集\n",
    "def load_data():\n",
    "    iris = datasets.load_iris()\n",
    "    \"\"\"展示数据集的形状\n",
    "       diabetes.data.shape, diabetes.target.shape\n",
    "    \"\"\"\n",
    "    \n",
    "    # 将数据集拆分为训练集和测试集 \n",
    "    X_train, X_test, y_train, y_test = train_test_split(\n",
    "    iris.data, iris.target, test_size=0.10, random_state=0)\n",
    "    return X_train, X_test, y_train, y_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用LinearSVC考察线性分类SVM的预测能力\n",
    "def test_LinearSVC(X_train,X_test,y_train,y_test):\n",
    "    \n",
    "    # 选择模型\n",
    "    cls = svm.LinearSVC()\n",
    "    \n",
    "    # 把数据交给模型训练\n",
    "    cls.fit(X_train,y_train)\n",
    "    \n",
    "    print('Coefficients:%s, intercept %s'%(cls.coef_,cls.intercept_))\n",
    "    print('Score: %.2f' % cls.score(X_test, y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Coefficients:[[ 0.18423407  0.45122519 -0.80794532 -0.45071298]\n",
      " [-0.1322586  -0.75229926  0.57184056 -1.11392435]\n",
      " [-0.79432899 -0.95799905  1.31471912  1.81709791]], intercept [ 0.10956131  1.86374068 -1.72587324]\n",
      "Score: 1.00\n"
     ]
    }
   ],
   "source": [
    "if __name__==\"__main__\":\n",
    "    X_train,X_test,y_train,y_test=load_data() # 生成用于分类的数据集\n",
    "    test_LinearSVC(X_train,X_test,y_train,y_test) # 调用 test_LinearSVC"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 1,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
